/**
 * Copyright © 2018 Beijing RainXing Technology. Tech Ltd. All rights reserved.
 *
 * @Package: com.rainxing.app.utils
 * @author: ybzhu
 * @date: 2018年6月25日 上午11:45:31
 */
package com.rainxing.cockpit.utils;

import java.math.RoundingMode;
import java.text.*;
import java.util.*;

/**
 * @ClassName: DataTools
 * @author: ybzhu
 * @date: 2018年6月25日 上午11:45:31
 */
public class DataTools {

    /**
     * 计算同比
     *
     * @param list
     * @param endList
     * @return
     */
    public static Map<String, Object> tbNew(List<Map<String, Object>> list, List<Map<String, Object>> endList) {
        Map<String, Object> tbMap = new HashMap<>();
//        for (int i = 0; i < list.size(); i++) {
//            List arr=new ArrayList();
//            Map<String, Object> tempMap = list.get(i);
//            String months = String.valueOf(tempMap.get("months")).substring(tempMap.get("months").toString().length() - 2);
//            for (int j = 0; j < endList.size(); j++) {
//                if(!tbMap.containsKey(tempMap.get("classeight").toString())){
//                    tbMap.put(tempMap.get("classeight").toString(),arr);
//                }
//                Map<String, Object> tempEndMap = endList.get(j);
//                String endMonths = String.valueOf(tempEndMap.get("months")).substring(tempEndMap.get("months").toString().length() - 2);
//                if ((tempMap.get("classeight").toString().equals(tempEndMap.get("classeight").toString())) && (months.equals(endMonths))) {
//
//                    int str = Integer.valueOf(String.valueOf(tempMap.get("counts")));
//                    int end = Integer.valueOf(String.valueOf(tempEndMap.get("counts")));
//                    //这个 counts是结果
//                    Double counts = Double.valueOf(str) - Double.valueOf(end);
//                    //这个是根据月份将结果写进map months 这个months 8大类有8个01 那你的值还没变啊months 不还是01
//                    //tempMap.get("classeight").toString().equals(tempEndMap.get("tempEndMap").toString()) 这个
//                    Map<String, String> map = new HashMap<>();
//                    map.put(tempMap.get("months").toString(), String.valueOf(counts));
//                    // tbMap.put(tempMap.get("classeight").toString(), map);你看我这个对吗
//                    //这个是将当前这个类型的map写进去
//                    List<Map>  tempArr= (List<Map>) tbMap.get(tempMap.get("classeight").toString());
//                    tempArr.add(map);
//                    tbMap.put(tempMap.get("classeight").toString(),tempArr);
//                }
//
//            }
//
//        }
        return tbMap;
    }

    public static List<Map<String, Object>> tbNew2(List<Map<String, Object>> list, List<Map<String, Object>> endList) {
        Map<String, Object> tbMap = new HashMap<>();
        List<Map<String, Object>> tbList = new ArrayList<>();
        for (int i = 0; i < list.size(); i++) {
            List arr = new ArrayList();
            Map<String, Object> tempMap = list.get(i);
            String months = String.valueOf(tempMap.get("months")).substring(tempMap.get("months").toString().length() - 2);
            for (int j = 0; j < endList.size(); j++) {
                if (!tbMap.containsKey(tempMap.get("classeight").toString())) {
                    tbMap.put(tempMap.get("classeight").toString(), arr);
                }
                Map<String, Object> tempEndMap = endList.get(j);
                String endMonths = String.valueOf(tempEndMap.get("months")).substring(tempEndMap.get("months").toString().length() - 2);
                if (tempMap.get("classeight").toString().equals(tempEndMap.get("classeight").toString())) {
                    if (months.equals(endMonths)) {
                        int str = Integer.valueOf(String.valueOf(tempMap.get("counts")));
                        int end = Integer.valueOf(String.valueOf(tempEndMap.get("counts")));
                        //这个 counts是结果
                        Double counts = Double.valueOf(str) - Double.valueOf(end);
                        Map<String, String> map = new HashMap<>();
                        map.put(tempMap.get("months").toString(), String.valueOf(counts));
                        List<Map> tempArr = (List<Map>) tbMap.get(tempMap.get("classeight").toString());
                        tempArr.add(map);
                        tbMap.put(tempMap.get("classeight").toString(), tempArr);
                        break;
                    }
                }
            }
        }
        tbList.add(tbMap);
        return tbList;
    }


    /**
     * 计算同比
     *
     * @param list
     * @param endList
     * @return
     */
    public static Map<String, String> tbAndHB(List<Map<String, Object>> list, List<Map<String, Object>> endList) {
        Map<String, String> map = new HashMap<>();
        for (Map<String, Object> m : list) {
            String nowMonths = String.valueOf(m.get("months")).substring(m.get("months").toString().length() - 2);
            Object obj = m.get("classeight");
            String classeight = null;
            if (obj != null) {
                classeight = m.get("classeight").toString();
            }
            for (Map<String, Object> hb : endList) {
                String endMonths = String.valueOf(hb.get("months")).substring(hb.get("months").toString().length() - 2);
                Object ob = m.get("classeight");
                String eight = null;
                if (ob != null) {
                    eight = hb.get("classeight").toString();
                }
                if (nowMonths.equals(endMonths)) {
                    int strCount = Integer.valueOf(String.valueOf(m.get("counts")));
                    int endCount = Integer.valueOf(String.valueOf(hb.get("counts")));
                    Double strMoney = 0.0;
                    Double endMoney = 0.0;
                    Double money = 0.0;
                    Double moneys = 0.0;
                    if (m.get("moneys") != null) {
                        strMoney = Double.valueOf(String.valueOf(m.get("moneys")));
                        endMoney = Double.valueOf(String.valueOf(hb.get("moneys")));
                        money = (Double.valueOf(strMoney) - Double.valueOf(endMoney)) / Double.valueOf(endMoney);
                        moneys = Double.valueOf(strCount) - Double.valueOf(endCount);
                    }
                    double count = (Double.valueOf(strCount) - Double.valueOf(endCount)) / Double.valueOf(endCount);
                    Double counts = Double.valueOf(strCount) - Double.valueOf(endCount);
                    if (classeight == null) {
                        map.put(m.get("months").toString(), DataTools.stringDouble(count));
                        map.put(nowMonths, String.valueOf(counts));
                        if (m.get("moneys") != null) {
                            map.put("m" + m.get("months").toString(), DataTools.stringDouble(money));
                            map.put("m" + nowMonths, String.valueOf(moneys));
                        }
                        break;
                    } else if (classeight.equals(eight)) {
                        map.put(classeight + ":" + m.get("months").toString(), DataTools.stringDouble(count));
                        map.put(classeight + ":" + nowMonths, String.valueOf(counts));
                        if (m.get("moneys") != null) {
                            map.put(classeight + "m:" + m.get("months").toString(), DataTools.stringDouble(money));
                            map.put(classeight + "m:" + nowMonths, String.valueOf(moneys));
                        }
                        break;
                    }
                }
            }


        }
        return map;
    }

    /**
     * 计算同比
     *
     * @param list
     * @param endList
     * @return
     */
    public static Map<String, String> tbAndHB2(List<Map<String, Object>> list, List<Map<String, Object>> endList) {
        Map<String, String> map = new HashMap<>();
        Double strMoney = 0.0;
        Double endMoney = 0.0;
        Double money = 0.0;
        Double moneys = 0.0;
        for (Map<String, Object> m : list) {
            String nowMonths = String.valueOf(m.get("months")).substring(m.get("months").toString().length() - 2);
            Object obj = m.get("classeight");
            String classeight = null;
            if (obj != null) {
                classeight = m.get("classeight").toString();
                for (Map<String, Object> hb : endList) {
                    String endMonths = String.valueOf(hb.get("months")).substring(hb.get("months").toString().length() - 2);
                    Object ob = hb.get("classeight");
                    if (ob != null) {
                        String eight = hb.get("classeight").toString();
                        if (classeight.equals(eight)) {
                            if (nowMonths.equals(endMonths)) {

                                int str = Integer.valueOf(String.valueOf(m.get("counts")));
                                int end = Integer.valueOf(String.valueOf(hb.get("counts")));
                                if (m.get("moneys") != null) {
                                    map.put("m" + m.get("months").toString(), DataTools.stringDouble(money));
                                    map.put("m" + nowMonths, String.valueOf(moneys));
                                    map.put(classeight + "m:" + m.get("moneys").toString(), DataTools.stringDouble(money));
                                    map.put(classeight + "m:" + nowMonths, String.valueOf(moneys));
                                }
                                double count = (Double.valueOf(str) - Double.valueOf(end)) / Double.valueOf(end);
                                Double counts = Double.valueOf(str) - Double.valueOf(end);
                                map.put(classeight + ":" + m.get("months").toString(), DataTools.stringDouble(count));
                                map.put(classeight + ":" + nowMonths, String.valueOf(counts));
                                break;
                            }
                        }
                    }
                }
            } else {
                for (Map<String, Object> hb : endList) {
                    String endMonths = String.valueOf(hb.get("months")).substring(hb.get("months").toString().length() - 2);
                    if (nowMonths.equals(endMonths)) {
                        int str = Integer.valueOf(String.valueOf(m.get("counts")));
                        int end = Integer.valueOf(String.valueOf(hb.get("counts")));
                        if (m.get("moneys") != null) {
                            map.put("m" + m.get("months").toString(), DataTools.stringDouble(money));
                            map.put("m" + nowMonths, String.valueOf(moneys));
                            map.put(classeight + "m:" + m.get("moneys").toString(), DataTools.stringDouble(money));
                            map.put(classeight + "m:" + nowMonths, String.valueOf(moneys));
                        }
                        double count = (Double.valueOf(str) - Double.valueOf(end)) / Double.valueOf(end);
                        Double counts = Double.valueOf(str) - Double.valueOf(end);
                        map.put(m.get("months").toString(), DataTools.stringDouble(count));
                        map.put(nowMonths, String.valueOf(counts));
                        break;
                    }
                }
            }
        }
        return map;
    }

    /**
     * 计算同比
     *
     * @param list
     * @param endList
     * @return
     */
    public static Map<String, String> tbAndHB3(List<Map<String, Object>> list, List<Map<String, Object>> endList) {
        Map<String, String> map = new HashMap<>();
        for (Map<String, Object> m : list) {
            String nowMonths = String.valueOf(m.get("months")).substring(m.get("months").toString().length() - 2);
            Object obj = m.get("classeight");
            String classeight = null;
            if (obj != null) {
                classeight = m.get("classeight").toString();
                for (Map<String, Object> hb : endList) {
                    String endMonths = String.valueOf(hb.get("months")).substring(hb.get("months").toString().length() - 2);
                    Object ob = hb.get("classeight");
                    if (ob != null) {
                        String eight = hb.get("classeight").toString();
                        if (classeight.equals(eight)) {
                            if (nowMonths.equals(endMonths)) {
                                int str = Integer.valueOf(String.valueOf(m.get("counts")));
                                int end = Integer.valueOf(String.valueOf(hb.get("counts")));
                                double count = (Double.valueOf(str) - Double.valueOf(end)) / Double.valueOf(end);
                                Double counts = Double.valueOf(str) - Double.valueOf(end);
                                map.put(classeight + ":" + m.get("months").toString(), DataTools.stringDouble(count));
                                map.put(classeight + ":" + nowMonths, String.valueOf(counts));
                                break;
                            }
                        }
                    }
                }
            } else {
                for (Map<String, Object> hb : endList) {
                    String endMonths = String.valueOf(hb.get("months")).substring(hb.get("months").toString().length() - 2);
                    if (nowMonths.equals(endMonths)) {
                        int str = Integer.valueOf(String.valueOf(m.get("counts")));
                        int end = Integer.valueOf(String.valueOf(hb.get("counts")));
                        double count = (Double.valueOf(str) - Double.valueOf(end)) / Double.valueOf(end);
                        Double counts = Double.valueOf(str) - Double.valueOf(end);
                        map.put(m.get("months").toString(), DataTools.stringDouble(count));
                        map.put(nowMonths, String.valueOf(counts));
                        break;
                    }
                }
            }
        }
        return map;
    }

    /**
     * 计算环比
     *
     * @param list
     * @param endList
     * @return
     */
    public static Map<String, String> hbAndHB(List<Map<String, Object>> list, List<Map<String, Object>> endList) {
        Map<String, String> map = new HashMap<>();
        for (int i = 0; i < list.size(); i++) {
            String mo = String.valueOf(list.get(i).get("months").toString().substring(list.get(i).get("months").toString().length() - 2));
            Object obj = list.get(i).get("classeight");
            String classeight = null;
            if (obj != null) {
                classeight = list.get(i).get("classeight").toString();
            }
            for (int j = 0; j < endList.size(); j++) {
                Object ob = list.get(i).get("classeight");
                String eight = null;
                if (ob != null) {
                    eight = endList.get(j).get("classeight").toString();
                }
                int strCount = Integer.valueOf(String.valueOf(list.get(i).get("counts")));
                int endCount = Integer.valueOf(String.valueOf(endList.get(j).get("counts")));
                Double strMoney = 0.0;
                Double endMoney = 0.0;
                Double money = 0.0;
                Double moneys = 0.0;
                if (list.get(i).get("moneys") != null) {
                    strMoney = Double.valueOf(String.valueOf(list.get(i).get("moneys")));
                    endMoney = Double.valueOf(String.valueOf(endList.get(j).get("moneys")));
                    money = (Double.valueOf(strMoney) - Double.valueOf(endMoney)) / Double.valueOf(endMoney);
                    moneys = Double.valueOf(strCount) - Double.valueOf(endCount);
                }
                double count = (Double.valueOf(strCount) - Double.valueOf(endCount)) / Double.valueOf(endCount);
                Double counts = Double.valueOf(strCount) - Double.valueOf(endCount);
                if (classeight == null) {
                    map.put(list.get(i).get("months").toString(), DataTools.stringDouble(count));
                    map.put(mo, String.valueOf(counts));
                    if (list.get(i).get("moneys") != null) {
                        map.put("m" + list.get(i).get("months").toString(), DataTools.stringDouble(money));
                        map.put("m" + mo, String.valueOf(moneys));
                    }
                    break;
                } else if (classeight.equals(eight)) {
                    map.put(classeight + ":" + list.get(i).get("months").toString(), DataTools.stringDouble(count));
                    map.put(classeight + ":" + mo, String.valueOf(counts));
                    if (list.get(i).get("moneys") != null) {
                        map.put(classeight + "m:" + list.get(i).get("months").toString(), DataTools.stringDouble(money));
                        map.put(classeight + "m:" + mo, String.valueOf(moneys));
                    }
                    break;
                }
            }
        }
        return map;
    }

    public static String[] startAndEnd() {
        String[] time = new String[2];
        Calendar now = Calendar.getInstance();
        time[0] = String.valueOf(now.get(Calendar.YEAR)) + "-01-01 00:00:00";
        SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");//设置日期格式
        System.out.println(df.format(new Date()));// new Date()为获取当前系统时间
        time[1] = df.format(new Date());
        return time;
    }

    public static String toUpTime(String date1) {
        DateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        Date d2 = new Date();
        try {
            Date d1 = df.parse(date1);
            Calendar g = Calendar.getInstance();
            g.setTime(d1);
            g.add(Calendar.MONTH, -1);
            d2 = g.getTime();
        } catch (ParseException e) {
            e.printStackTrace();
        }
        return df.format(d2);
    }

    /**
     * 将字符串20180123转换为2018-01-23日期格式 针对一个的操作
     *
     * @param
     * @return
     * @throws ParseException
     */
    public static String StrToDate(String str) throws ParseException {
        SimpleDateFormat dateFormat = new SimpleDateFormat("yyyyMMdd");
        SimpleDateFormat dateFormat2 = new SimpleDateFormat("yyyy-MM-dd");
        String result = dateFormat2.format(dateFormat.parse(str));
        // System.err.println(result);
        return result;

    }

    //替换2018 字段
    public static String upTime(String str) {
        String[] strs = str.split("-");
        int year = Integer.valueOf(strs[0].toString()) - 1;
        String up = str.replace(strs[0].toString(), String.valueOf(year));
        return up;
    }

    /**
     * 将2018-01-23的字符串转换成20180123的格式 针对一个日期的操作
     *
     * @param dateStr
     * @return
     * @throws ParseException
     */
    public static String DateToStr(String dateStr) throws ParseException {
        SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
        SimpleDateFormat dateFormat2 = new SimpleDateFormat("yyyyMMdd");
        Date parse = dateFormat.parse(dateStr);
        String format = dateFormat2.format(parse);
        return format;

    }

    /**
     * 将yyyy-MM-dd HH:mm:ss字符串转换成日期(net.maxt.util.Date)<br/>
     *
     * @param dateStr yyyy-MM-dd HH:mm:ss字符串
     * @return net.maxt.util.Date 日期 ,转换异常时返回null。
     */
    public static Date parseDate(String dateStr) {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        try {
            Date d = sdf.parse(dateStr);
            return new Date(d.getTime());
        } catch (ParseException e) {
            e.printStackTrace();
            return null;
        }
    }

    /**
     * 将Date类型时间转换为字符串
     *
     * @param date
     * @return
     */
    public static String toString(Date date) {

        String time;
        SimpleDateFormat formater = new SimpleDateFormat();
        formater.applyPattern("yyyy-MM-dd HH:mm:ss");
        time = formater.format(date);
        return time;
    }

    /**
     * 功能描述:
     * 输入指定日期和相差天数，得到具体某一天
     *
     * @param:
     * @return:
     * @auther:
     * @date:
     */
    public static String StringtoString(String str, int days) {
        // 时间表示格式可以改变，yyyyMMdd需要写例如20160523这种形式的时间
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
//		String str = "2018-01-01 00:00:00";
        // 将字符串的日期转为Date类型，ParsePosition(0)表示从第一个字符开始解析
        Date date = sdf.parse(str, new ParsePosition(0));
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(date);
        // add方法中的第二个参数n中，正数表示该日期后n天，负数表示该日期的前n天
        calendar.add(Calendar.DATE, days);
        Date date1 = calendar.getTime();
        String out = sdf.format(date1);
        return out;
    }

    /**
     * 功能描述:
     * 计算天数
     *
     * @param:
     * @return:
     * @auther:
     * @date:
     */
    public static Date dateAdd(int days) {
        // 日期处理模块 (将日期加上某些天或减去天数)返回字符串
        Calendar canlendar = Calendar.getInstance(); // java.util包
        canlendar.add(Calendar.DATE, days); // 日期减 如果不够减会将月变动
        return canlendar.getTime();
    }

    /**
     * 这个方法挺简单的。
     * DecimalFormat is a concrete subclass of NumberFormat that formats decimal numbers.
     *
     * @param d
     * @return
     */
    public static String formatDouble4(double d) {
        DecimalFormat df = new DecimalFormat("#.0000");


        return df.format(d);
    }

    /**
     * 保留两位小数，四舍五入的一个老土的方法
     *
     * @param d
     * @return
     */
    public static double formatDouble1(double d) {
        return (double) Math.round(d * 100) / 100;
    }

    /**
     * 功能描述:
     * 对double类型保留两位小数
     *
     * @param:
     * @return:
     * @auther:
     * @date:
     */
    public static String stringDouble(double dou) {
        NumberFormat nf = NumberFormat.getPercentInstance();
        nf.setMinimumFractionDigits(2);//设置保留小数位
        nf.setRoundingMode(RoundingMode.HALF_UP); //设置舍入模式
        String percent = nf.format(dou);
        return percent;
    }

    /**
     * 计算两个数的百分比
     *
     * @param num1
     * @param num2
     * @return
     */
    public static String doubleString(int num1, int num2) {
        // 创建一个数值格式化对象
        NumberFormat numberFormat = NumberFormat.getInstance();
        // 设置精确到小数点后2位
        numberFormat.setMaximumFractionDigits(2);
        String result = numberFormat.format((float) num1 / (float) num2 * 100);
        String re = result + "%";
        return re;
    }

    /**
     * double
     *
     * @param num1
     * @param num2
     * @return
     */
    public static String doubleTodouble(double num1, double num2) {
        // 创建一个数值格式化对象
        NumberFormat numberFormat = NumberFormat.getInstance();
        // 设置精确到小数点后2位
        numberFormat.setMaximumFractionDigits(2);
        String result = numberFormat.format(num1 / num2 * 100);
        String re = result + "%";
        return re;
    }

    /**
     * 计算百分比
     *
     * @param num1
     * @param num2
     * @return
     */
    public static String flotString(float num1, float num2) {
        // 创建一个数值格式化对象
        NumberFormat numberFormat = NumberFormat.getInstance();
        // 设置精确到小数点后2位
        numberFormat.setMaximumFractionDigits(2);
        String result = numberFormat.format((float) num1 / (float) num2 * 100);
        return result + "%";
    }

    /**
     * double
     *
     * @param num1
     * @param num2
     * @return
     */
    public static String doubleString2(double num1, double num2) {
        // 创建一个数值格式化对象
        NumberFormat numberFormat = NumberFormat.getInstance();
        // 设置精确到小数点后2位
        numberFormat.setMaximumFractionDigits(2);
        String result = numberFormat.format(num1 / num2);
        return result;
    }

    public static String floatString(float num1, double num2) {
        // 创建一个数值格式化对象
        NumberFormat numberFormat = NumberFormat.getInstance();
        // 设置精确到小数点后2位
        numberFormat.setMaximumFractionDigits(2);
        String result = numberFormat.format(num1 / num2);
        return result;
    }

    public static double doubleString3(double num1, double num2) {
        // 创建一个数值格式化对象
        NumberFormat numberFormat = NumberFormat.getInstance();
        // 设置精确到小数点后2位
        numberFormat.setMaximumFractionDigits(2);
        String result = numberFormat.format(num1 / num2);
        return Double.parseDouble(result);
    }


    /**
     * 两个时间之间的天数 初始化的格式 2018-01-02 long days = getDays("2018-01-22",
     * "2018-01-02");
     *
     * @param date1 2018-01-22
     * @param date2 2018-01-02
     * @return
     */
    public static long getDays(String date1, String date2) {
        if (date1 == null || date1.equals(""))
            return 0;
        if (date2 == null || date2.equals(""))
            return 0;
        // 转换为标准时间
        SimpleDateFormat myFormatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        Date date = null;
        Date mydate = null;
        try {
            date = myFormatter.parse(date1);
            mydate = myFormatter.parse(date2);
        } catch (Exception e) {
        }
        long day = (date.getTime() - mydate.getTime()) / (24 * 60 * 60 * 1000);
        return day;
    }

    /****
     * 传入具体日期 ，返回具体日期减一个月。
     *
     * @param date
     *            日期(2014-04-20)
     * @return 2014-03-20
     * @throws ParseException
     */
    public static String subMonth(String date) {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        Date dt = null;
        try {
            dt = sdf.parse(date);
        } catch (ParseException e) {
            e.printStackTrace();
        }
        Calendar rightNow = Calendar.getInstance();
        rightNow.setTime(dt);

        rightNow.add(Calendar.MONTH, -1);
        Date dt1 = rightNow.getTime();
        String reStr = sdf.format(dt1);

        return reStr;
    }

    public static String subMonth2(String date) {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        Date dt = null;
        try {
            dt = sdf.parse(date);
        } catch (ParseException e) {
            e.printStackTrace();
        }
        Calendar rightNow = Calendar.getInstance();
        rightNow.setTime(dt);

        rightNow.add(Calendar.MONTH, -1);
        Date dt1 = rightNow.getTime();
        String reStr = sdf.format(dt1);

        return reStr;
    }


    /**
     * 将短时间格式字符串转换为时间 yyyy-MM-dd
     *
     * @param strDate
     * @return
     */
    public static Date strToDate(String strDate) {
        SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd");
        ParsePosition pos = new ParsePosition(0);
        Date strtodate = formatter.parse(strDate, pos);
        return strtodate;
    }

    /**
     * 根据一个日期，返回是星期几的字符串
     *
     * @param sdate
     * @return
     */
    public static String getWeek(String sdate) {
        // System.out.println("sdate--->>>"+sdate);
        // 再转换为时间
        Date date = strToDate(sdate);
        Calendar c = Calendar.getInstance();
        c.setTime(date);
        // int hour=c.get(Calendar.DAY_OF_WEEK);
        // hour中存的就是星期几了，其范围 1~7
        // 1=星期日 7=星期六，其他类推
        return new SimpleDateFormat("EEEE").format(c.getTime());
    }

    /**
     * 获取起止日期
     *
     * @param sdf    需要显示的日期格式
     * @param date   需要参照的日期
     * @param n      最近n周
     * @param option 0 开始日期；1 结束日期
     * @param k      0 包含本周 1 不包含本周
     * @return
     */
    public static String getFromToDate(SimpleDateFormat sdf, Date date, int n, int option, int k) {
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(date);
        int dayOfWeek = calendar.get(Calendar.DAY_OF_WEEK) - 1;
        int offset = 0 == option ? 1 - dayOfWeek : 7 - dayOfWeek;
        int amount = 0 == option ? offset - (n - 1 + k) * 7 : offset - k * 7;
        calendar.add(Calendar.DATE, amount);
        return sdf.format(calendar.getTime());
    }

    /**
     * 根据当前日期获得最近n周的日期区间（包含本周）
     *
     * @param n
     * @param sdf
     * @return
     */
    public static String getNWeekTimeInterval(int n, SimpleDateFormat sdf) {
        String beginDate = getFromToDate(sdf, new Date(), n, 0, 0);
        String endDate = getFromToDate(sdf, new Date(), n, 1, 0);
        return beginDate + "," + endDate;
    }

    /**
     * 根据当前日期获得最近n周的日期区间（不包含本周）
     *
     * @param n
     * @param sdf
     * @return
     */
    public static String getNWeekTimeIntervalTwo(int n, SimpleDateFormat sdf) {
        String beginDate = getFromToDate(sdf, new Date(), n, 0, 1);
        String endDate = getFromToDate(sdf, new Date(), n, 1, 1);
        String begin = " 00:00:00";
        String end = " 23:59:59";
        return beginDate + begin + "," + endDate + end;
    }

    /**
     * 根据当前日期获得上上周周的日期区间
     *
     * @param n
     * @param sdf
     * @return
     */
    public static String getNWeekTimeIntervalThree(int n, SimpleDateFormat sdf) {
        String beginDate = getFromToDate(sdf, new Date(), n, 0, 1);
        String endDate = getFromToDate(sdf, new Date(), n, 1, 2);
        String begin = " 00:00:00";
        String end = " 23:59:59";
        return beginDate + begin + "," + endDate + end;
    }

    /**
     * 根据当前日期获得本周的日期区间（本周周一和周日日期）
     *
     * @param sdf
     * @return
     */
    public static String getThisWeekTimeInterval(SimpleDateFormat sdf) {
        return getNWeekTimeInterval(1, sdf);
    }

    /**
     * 根据当前日期获得上周的日期区间（上周周一和周日日期）
     *
     * @param sdf
     * @return
     */
    public static String getLastWeekTimeInterval(SimpleDateFormat sdf) {
        return getNWeekTimeIntervalTwo(1, sdf);
    }

    /**
     * 根据当前日期获得上上周周的日期区间
     *
     * @param sdf
     * @return
     */
    public static String getLastWeekTimeInterva2(SimpleDateFormat sdf) {
        return getNWeekTimeIntervalThree(2, sdf);
    }

    public static void main(String[] args) {
//        System.out.println(getThisWeekTimeInterval(new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"))); // 获得本周的日期区间
        System.out.println(getLastWeekTimeInterval(new SimpleDateFormat("yyyy-MM-dd"))); // 获得上周的日期区间
        System.out.println(getLastWeekTimeInterva2(new SimpleDateFormat("yyyy-MM-dd"))); // 获得上周的日期区间
    }
}
